package de.hs.rm.UJAdapter.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.TabularDataSupport;

import org.osgi.service.upnp.UPnPStateVariable;

import de.hs.rm.UJAdapter.impl.UJStateVariable;

public final class ClassTypeUtil {
	
	public static final int OPENTYPE_SIMPLE = 0;
	public static final int OPENTYPE_ARRAY = 1;
	public static final int OPENTYPE_COMPOSITE = 2;
	public static final int OPENTYPE_TABULAR = 3;
	
	public static final int OPENDATA_SIMPLE = 0;
	public static final int OPENDATA_ARRAY = 1;
	public static final int OPENDATA_COMPOSITE = 2;
	public static final int OPENDATA_TABULAR = 3;
	
	public static Class getJavaDataTypeFromString(String dataType)
	{
		if(dataType.contains("String"))
			return String.class;		
		if(dataType.contains("boolean"))
			return Boolean.class;
		if(dataType.contains("int"))
			return Integer.class;
		if(dataType.contains("long"))
			return Long.class;
		if(dataType.contains("float"))
			return Float.class;
		if(dataType.contains("double"))
			return Double.class;
		if(dataType.contains("char"))
			return Character.class;
		if(dataType.contains("byte"))
			return Byte[].class;
		if(dataType.contains("ObjectName"))
			return String.class;
//		if(dataType.contains("[Ljava"))
//			return javax.management.openmbean.ArrayType.class;
//		if(dataType.contains("TabularData"))
//			return TabularData.class;
//		if(dataType.contains("CompositeData"))
//			return CompositeData.class;
		return String.class;
		
	}
	
	public static int getOpenType(String openTypeClassString)
	{
		openTypeClassString = openTypeClassString.substring(0,openTypeClassString.indexOf("("));
		if(openTypeClassString.contains("SimpleType"))
			return OPENTYPE_SIMPLE;
		if(openTypeClassString.contains("ArrayType"))
			return OPENTYPE_ARRAY;
		if(openTypeClassString.contains("CompositeType"))
			return OPENTYPE_COMPOSITE;
		if(openTypeClassString.contains("TabularType"))
			return OPENTYPE_TABULAR;
		else
			return -1;
	}
	
	public static int getOpenData(Object openData)
	{
		if(openData.getClass().isArray())
			return OPENDATA_ARRAY;
		if(openData.getClass() == CompositeDataSupport.class)
			return OPENDATA_COMPOSITE;
		if(openData.getClass() == TabularDataSupport.class)
			return OPENDATA_TABULAR;
		else
			return OPENDATA_SIMPLE;
	}
	
	public static String getUPNPDataTypeFromString(String dataType)
	{
		if(dataType.contains("String"))
			return UPnPStateVariable.TYPE_STRING;		
		if(dataType.contains("boolean"))
			return UPnPStateVariable.TYPE_BOOLEAN;
		if(dataType.contains("int"))
			return UPnPStateVariable.TYPE_INT;
		if(dataType.contains("long"))
			return UPnPStateVariable.TYPE_INT;
		if(dataType.contains("float"))
			return UPnPStateVariable.TYPE_FLOAT;
		if(dataType.contains("double"))
			return UPnPStateVariable.TYPE_R8;
		if(dataType.contains("char"))
			return UPnPStateVariable.TYPE_CHAR;
		if(dataType.contains("byte"))
			return UPnPStateVariable.TYPE_BIN_HEX;
		if(dataType.contains("date"))
			return UPnPStateVariable.TYPE_DATE;
		if(dataType.contains("ObjectName"))
			return UPnPStateVariable.TYPE_STRING;
		return UPnPStateVariable.TYPE_STRING; //Test		
	}
	
	public static String getUPnPDataTypefromSimpleType(String simpleType)
	{
		if(simpleType.equals("java.lang.Boolean"))
			return UPnPStateVariable.TYPE_BOOLEAN;
        if(simpleType.equals("java.lang.Character"))
			return UPnPStateVariable.TYPE_CHAR;
        if(simpleType.equals("java.lang.Byte"))
			return UPnPStateVariable.TYPE_BIN_HEX;
        if(simpleType.equals("java.lang.Short"))
			return UPnPStateVariable.TYPE_INT;
        if(simpleType.equals("java.lang.Integer"))
			return UPnPStateVariable.TYPE_INT;
        if(simpleType.equals("java.lang.Long"))
			return UPnPStateVariable.TYPE_INT;
        if(simpleType.equals("java.lang.Float"))
			return UPnPStateVariable.TYPE_FLOAT;
        if(simpleType.equals("java.lang.Double"))
			return UPnPStateVariable.TYPE_R8;
        if(simpleType.equals("java.lang.String"))
			return UPnPStateVariable.TYPE_STRING;
        if(simpleType.equals("java.math.BigDecimal"))
			return UPnPStateVariable.TYPE_R8;
        if(simpleType.equals("java.math.BigInteger"))
			return UPnPStateVariable.TYPE_INT;
        if(simpleType.equals("java.util.Date"))
			return UPnPStateVariable.TYPE_DATE;
        if(simpleType.equals("javax.management.ObjectName"))
			return UPnPStateVariable.TYPE_STRING;
        return UPnPStateVariable.TYPE_STRING;
	}
	
	public static Class getJavaDataTypefromSimpleType(String simpleType)
	{
		if(simpleType.equals("java.lang.Boolean"))
			return Boolean.class;
        if(simpleType.equals("java.lang.Character"))
			return Character.class;
        if(simpleType.equals("java.lang.Byte"))
			return Byte.class;
        if(simpleType.equals("java.lang.Short"))
			return Short.class;
        if(simpleType.equals("java.lang.Integer"))
			return Integer.class;
        if(simpleType.equals("java.lang.Long"))
			return Long.class;
        if(simpleType.equals("java.lang.Float"))
			return Float.class;
        if(simpleType.equals("java.lang.Double"))
			return Double.class;
        if(simpleType.equals("java.lang.String"))
			return String.class;
        if(simpleType.equals("java.math.BigDecimal"))
			return BigDecimal.class;
        if(simpleType.equals("java.math.BigInteger"))
			return BigInteger.class;
        if(simpleType.equals("java.util.Date"))
			return Date.class;
        if(simpleType.equals("javax.management.ObjectName"))
			return ObjectName.class;
        return String.class;
	}
	
	public static boolean checkListForOpenType(ArrayList<String> aList)
	{
		String openTypeClassString = "";
		for (int i = 0; i < aList.size(); i++)
		{
			openTypeClassString = aList.get(i);
			if(openTypeClassString.contains("openmbean"))
				return true;
//			if(openTypeClassString.contains("ArrayType"))
//				return true;
//			if(openTypeClassString.contains("CompositeType"))
//				return true;
//			if(openTypeClassString.contains("TabularType"))
//				return true;
		}
		return false;
	}
}
